package httpSessionParam;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

/**
 * @author fhzheng
 * <li>添加购物车
 *
 */
public class AddServlet extends HttpServlet
{
	public void init(){}
	public void doGet(
			HttpServletRequest request,
			HttpServletResponse response) 
					throws ServletException,IOException {
		// 配置输出参数
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		// 输出对象，即开流
		PrintWriter out=response.getWriter();
		// 创建session对象，即创建会话
		HttpSession session=request.getSession();
		// 获取参数，强转为相应的类型，更方便使用
		String product=(String)request.getParameter("product");
		String shopping=(String)session.getAttribute("shopping");
		// 设置参数的编码，避免中文乱码
        String newproduct=new String(product.getBytes(),"utf-8");
		if(shopping==null)	// 首次，肯定是null状态
		{
			//设置session域的属性参数值
			session.setAttribute("shopping",newproduct); 		
		}
		else
		{
			String str=shopping+"<br>"+newproduct;
			session.setAttribute("shopping",str);
		}
		out.println("产品已添加至购物车中！");
		out.close();
	}
}